<script>
import { onMounted, reactive, ref } from "vue";
import { Toast } from "vant";
export default {
  setup() {
    const show = ref(false);
    const shows = ref(false);
    const columns = ["保密", "男", "女"];
    const currentDate = ref(new Date());
    const changeinfo = reactive({
      name: "Mercurio",
      sex: "女",
      birthday: "2022-11-09",
      //   columns: ["保密", "男", "女"],

      //   currentDate: new Date(),
    });
    onMounted(() => {
      changeinfo.name = window.localStorage.getItem("name");
      changeinfo.sex = window.localStorage.getItem("sex");
      changeinfo.birthday = window.localStorage.getItem("birthday");
    });
    const Change = () => {
      window.localStorage.setItem("name", changeinfo.name);
      window.localStorage.setItem("sex", changeinfo.sex);
      window.localStorage.setItem("birthday", changeinfo.birthday);
      Toast("修改成功");
    };
    const showPopup = () => {
      show.value = true;
    };
    const onConfirm = (gender) => {
      changeinfo.sex = gender;
      show.value = false;
    };
    const onCancel = () => {
      Toast("取消");
      show.value = false;
    };
    const showPopups = () => {
      shows.value = true;
    };
    const onConfirms = (date) => {
      getSimpleDate(date);
      // console.log(changeinfo.getSimpleDate(date));
      // console.log(changeinfo.getSimpleDate(date).split(" ")[0]);
      changeinfo.birthday = getSimpleDate(date).split(" ")[0];
      shows.value = false;
    };
    const onCancels = () => {
      Toast("取消");
      shows.value = false;
    };
    const getSimpleDate = (date) => {
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      var d = date.getDate();
      d = d < 10 ? "0" + d : d;
      var h = date.getHours();
      var minute = date.getMinutes();
      minute = minute < 10 ? "0" + minute : minute;
      var s = date.getSeconds();
      s = s < 10 ? "0" + s : s;
      return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + s;
    };
    return {
      changeinfo,
      show,
      shows,
      Change,
      showPopup,
      onConfirm,
      onCancel,
      showPopups,
      onConfirms,
      onCancels,
      getSimpleDate,
      columns,
      currentDate,
      minDate: new Date(1000, 1, 1),
      maxDate: new Date(2022, 12, 1),
    };
  },
};
</script>

<template>
  <div class="container changeinfo">
    <div class="top">
      <i class="iconfont icon-zuobian" @click="$router.go(-1)"></i>
      <span class="title">个人资料</span>
      <div class="save">
        <i class="iconfont icon-baocun"></i>
        <span @click="Change">保存</span>
      </div>
    </div>

    <!-- 个人资料 -->
    <ul class="info">
      <li>
        <div class="left">头像</div>
        <div class="right">
          <div class="img">
            <img src="../assets/img/user.jpg" alt="" />
          </div>
          <i class="iconfont icon-youbian"></i>
        </div>
      </li>
      <li>
        <div class="left">昵称</div>
        <div class="right">
          <input type="text" name="" id="" v-model="changeinfo.name" />
          <!-- <span>{{changeinfo.name}}</span> -->
          <i class="iconfont icon-youbian"></i>
        </div>
      </li>
      <li>
        <div class="left">性别</div>
        <div class="right">
          <span @click="showPopup">{{ changeinfo.sex }}</span>
          <van-popup
            v-model:show="show"
            round
            position="bottom"
            :style="{ height: '30%' }"
          >
            <van-picker
              title="性别"
              show-toolbar
              :columns="columns"
              :default-index="2"
              @confirm="onConfirm"
              @cancel="onCancel"
            />
          </van-popup>

          <i class="iconfont icon-youbian"></i>
        </div>
      </li>
      <li>
        <div class="left">生日</div>
        <div class="right">
          <van-popup
            v-model:show="shows"
            position="bottom"
            round
            :style="{ height: '40%' }"
          >
            <van-datetime-picker
              v-model="currentDate"
              type="date"
              title="选择年月日"
              :min-date="minDate"
              :max-date="maxDate"
              @confirm="onConfirms"
              @cancel="onCancels"
            />
          </van-popup>
          <span @click="showPopups">{{ changeinfo.birthday }}</span>
          <i class="iconfont icon-youbian"></i>
        </div>
      </li>
    </ul>
  </div>
</template>

<style lang="scss" scoped>
.changeinfo {
  .top {
    height: 49px;
    height: 49px;
    padding: 5px 10px;
    background: linear-gradient(270deg, #7b0b3b 0%, #d51c78 100%);
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
    .iconfont {
      color: #fff;
      font-size: 20px;
    }
    .title {
      margin-left: 30px;
      font-size: 18px;
      color: #fff;
    }
    .save {
      display: flex;
      span {
        font-size: 16px;
        color: #fff;
      }
    }
  }
  .info {
    li {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 15px;
      border-bottom: 1px solid #ddd;
      .left {
        font-size: 18px;
      }
      .right {
        display: flex;
        align-items: center;
        img {
          width: 50px;
          height: 50px;
          border-radius: 50%;
        }
        .iconfont {
          color: #cdcdcd;
          margin-left: 10px;
        }
        input {
          text-align: right;
        }
      }
    }
  }
}
</style>